package com.leetcode.partition1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/8/13 10:03
 */
public class LC17电话号码的字母组合 {

    public static List<String> letterCombinations(String digits) {
        String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        List<String> answer = new ArrayList<>();
        if (digits.length() == 0) return answer;
        backtracking(digits, 0, letters, new StringBuilder(), answer);
        return answer;
    }

    public static void main(String[] args) {
        String digits = "23";
        System.out.println(letterCombinations(digits));
    }

    private static void backtracking(String digits, int digitsIndex, String[] letters, StringBuilder sb, List<String> answer) {
        if (digitsIndex == digits.length()) {
            answer.add(sb.toString());
            return;
        }
        //得到当前数字映射的所有字符集
        String letter = letters[digits.charAt(digitsIndex) - '0'];
        for (int i = 0; i < letter.length(); i++) {
            sb.append(letter.charAt(i));
            backtracking(digits, digitsIndex + 1, letters, sb, answer);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
